package com.example.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import com.example.common.Result;
import com.example.exception.CustomerException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/files")
public class FileController {
    @GetMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) throws Exception {
        String filePath=System.getProperty("user.dir")+"/files/";
        String realPath=filePath+fileName;
        boolean exist= FileUtil.exist(realPath);
        if(!exist){
            throw new CustomerException("文件不存在");
        }
        byte[] bytes = FileUtil.readBytes(realPath);
        ServletOutputStream os=response.getOutputStream();
        os.write(bytes);
        os.flush();
        os.close();
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile file) throws Exception{
        String filePath=System.getProperty("user.dir")+"/files/";
        if(!FileUtil.isDirectory(filePath)){
            FileUtil.mkdir(filePath);
        }
        byte[] bytes = file.getBytes();
        String filename =System.currentTimeMillis()+"_"+file.getOriginalFilename();
//        String encodeFileName = URLEncoder.encode(filename, StandardCharsets.UTF_8);

        FileUtil.writeBytes(bytes,filePath+filename);
        String url="http://localhost:9999/files/download/"+filename;
        return Result.success(url);
    }

    @PostMapping("/wang/upload")
    public Map<String, Object> wangEditorUpload(MultipartFile file){
        String flag=System.currentTimeMillis()+"";
        String fileName=file.getOriginalFilename();
        try{
            String filePath=System.getProperty("user.dir")+"/files/";
            FileUtil.writeBytes(file.getBytes(),filePath+flag+"-"+fileName);
            System.out.println(fileName+"--上传成功");
            Thread.sleep(1L);
        }catch (Exception e){
            System.err.println(fileName+"--上传失败");
        }
        String http="http://localhost:9999/files/download/";
        Map<String,Object> resMap=new HashMap<>();
        resMap.put("errno",0);
        resMap.put("data", CollUtil.newArrayList(Dict.create().set("url",http+flag+"-"+fileName)));
        return resMap;
    }


}
